package cn.bdqn.controller;

import cn.bdqn.mapper.StudentMapper;
import cn.bdqn.pojo.Student;
import cn.bdqn.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {

    //登录
    public void login(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String loginPassword = request.getParameter("loginPassword");
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        Student student = sqlSession.getMapper(StudentMapper.class).getStudentByName(name,loginPassword);
        if (student != null){
            response.sendRedirect(request.getContextPath()+"/student/ShowAllStudent");
        }else {
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }


    //查找所有学生
    public void ShowAllStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        List<Student> studentList = sqlSession.getMapper(StudentMapper.class).getAllStudent();
        request.setAttribute("studentList",studentList);
        request.getRequestDispatcher("/showAllStudent.jsp").forward(request,response);
    }


    //更新学生信息
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //收参
        Integer id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        String loginPassword = request.getParameter("loginPassword");
        String gender = request.getParameter("gender");
        Integer age = Integer.valueOf(request.getParameter("age"));
        Student student = new Student(id,name,loginPassword,gender,age);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        sqlSession.getMapper(StudentMapper.class).updateStudent(student);

        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        //最后重定向到ShowAllEmpController类
        response.sendRedirect(request.getContextPath() + "/student/ShowAllStudent");
    }


    //通过id查找学生，通过id修改学生信息
    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer id = Integer.valueOf(request.getParameter("id"));
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        Student student = sqlSession.getMapper(StudentMapper.class).getStudentById(id);
        request.setAttribute("student",student);
        MyBatisUtil.closeSqlSession(sqlSession);
        request.getRequestDispatcher("/studentUpdate.jsp").forward(request,response);
    }


    //通过id删除一个学生
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer id = Integer.valueOf(request.getParameter("id"));

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        sqlSession.getMapper(StudentMapper.class).deleteStudent(id);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        response.sendRedirect(request.getContextPath() + "/student/ShowAllStudent");

    }


    //添加一位学生
    public void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //收参
        String name = request.getParameter("name");
        String loginPassword = request.getParameter("loginPassword");
        String gender = request.getParameter("gender");
        Integer age = Integer.valueOf(request.getParameter("age"));
        Student student = new Student(name,loginPassword,gender,age);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        sqlSession.getMapper(StudentMapper.class).addStudent(student);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        response.sendRedirect(request.getContextPath() + "/student/ShowAllStudent");
    }

}
